#include "aos/aos.h"
#include "aos/kernel.h"
#include "app_main.h"

#include "board_config.h"
#include "drv/gpio_pin.h"
#include <drv/pin.h>
#include <aos/debug.h>

#include <drv/rtc.h>
#include "aos/hal/rtc.h"

//#include "oled.h"
//#include "oledfont.h"

#define TAG "APP_BUTTON"

uint8_t ddflag=0;
static csi_gpio_pin_t button;
extern aos_sem_t weather_sem;
rtc_dev_t rtc1;

rtc_time_t old_time, new_time;
uint8_t oled_buf[30];

static void button_task(void *argv)
{
	while(1){
		if(GPIO_PIN_LOW == csi_gpio_pin_read(&button)) {//持续按键置标志
			LOGD(TAG, "push down the key1\r\n ");
			
			aos_sem_signal(&weather_sem);
			ddflag = 1;
			
		} 
		aos_msleep(700);
	}
	
}

static void button_start(void)
{
    aos_task_t task;
    if (aos_task_new_ext(&task, "button", button_task, NULL, 1024, AOS_DEFAULT_APP_PRI + 3) != 0) {
        return;
    }
    LOGD(TAG, "creat task:button successful\r\n ");
    return;
}

void app_button_init(void)
{
    LOGD(TAG, "app_button_init\r\n ");
    csi_pin_set_mux(PA11, PIN_FUNC_GPIO);
    csi_gpio_pin_init(&button, PA11);
    csi_gpio_pin_dir(&button, GPIO_DIRECTION_INPUT);

	button_start();

}

void rtc_init(void)
{

    rtc_time_t time_buf;

    /* rtc port set */
    rtc1.port = 0;
    /* set to DEC format */
    rtc1.config.format = HAL_RTC_FORMAT_DEC;
    hal_rtc_init(&rtc1);

    time_buf.sec = 0;
    time_buf.min = 0;
    time_buf.hr = 12;
    time_buf.weekday = 2;
    time_buf.date = 4;
    time_buf.month = 5;
    time_buf.year = 22;
    /* set rtc1 time to 2022/4/5,11:03:05 */
    hal_rtc_set_time(&rtc1, &time_buf);
}


    void Display_Time(void)
	{
		//int wordcolor = 0;

    //while(1)
	//{
    //hal_rtc_get_time(&rtc1, &new_time);
	//if(old_time.sec != new_time.sec) {
	    //sprintf(oled_buf, "%02d-%02d-%02d", new_time.year, new_time.month, new_time.date);
		//ssd1306_SetCursor(2, 18+18);
        //ssd1306_WriteString(&oled_buf, Font_7x10, wordcolor);
	    //show_string(40, 8, oled_buf);
	//    sprintf(oled_buf, "%02d:%02d:%02d", new_time.hr, new_time.min, new_time.sec);
	    //show_string(40, 24, oled_buf);
	//	ssd1306_SetCursor(2, 18+18);
    //    ssd1306_WriteString(&oled_buf, Font_7x10, 0);
	//    old_time.sec = new_time.sec;
	
	
	//}
	//}
	}